FROM {{ namespace }}/{{ infra_image_prefix }}base:{{ tag }}
{% block labels %}
LABEL maintainer="{{ maintainer }}" name="{{ image_name }}" build-date="{{ build_date }}"
{% endblock %}

{% set fluentd_user = 'td-agent' %}

{% block fluentd_header %}{% endblock %}

{% import "macros.j2" as macros with context %}

{{ macros.enable_extra_repos(['td-agent']) }}

{% if base_package_type == 'rpm' %}

    {% set fluentd_packages = [
        'gcc-c++',
        'make',
        'td-agent',
    ] %}

{% elif base_package_type == 'deb' %}

    {% set fluentd_packages = [
        'g++',
        'make',
        'td-agent',
    ] %}

{% endif %}

{{ macros.configure_user(name=fluentd_user, groups='mysql') }}

# NOTE(frickler): Downgrading elasticsearch gems for compatibility with OSS versions of ES
{{ macros.install_packages(fluentd_packages | customizable("packages")) }} \
    && td-agent-gem install elasticsearch:7.13.0 \
    && td-agent-gem uninstall elasticsearch:7.15.0 \
    && td-agent-gem uninstall elasticsearch-api:7.15.0 \
    && td-agent-gem uninstall elasticsearch-transport:7.15.0

COPY fluentd_sudoers /etc/sudoers.d/kolla_fluentd_sudoers

RUN chmod 440 /etc/sudoers.d/kolla_fluentd_sudoers \
    && mkdir -p /etc/{{ fluentd_user }} \
    && mkdir -p /var/run/{{ fluentd_user }} \
    && chown -R {{ fluentd_user }}: /etc/{{ fluentd_user }} /var/run/{{ fluentd_user }}

COPY extend_start.sh /usr/local/bin/kolla_extend_start
RUN chmod 755 /usr/local/bin/kolla_extend_start

{% block fluentd_plugins_install %}

{% set fluentd_plugins = [
    'fluent-plugin-elasticsearch',
    'fluent-plugin-grok-parser',
    'fluent-plugin-prometheus',
    'fluent-plugin-rewrite-tag-filter',
] %}

{{ macros.install_fluent_plugins(fluentd_plugins | customizable("plugins")) }}

{% endblock %}

{% block fluentd_monasca_plugin_install %}
# Build and install Fluentd output plugin for Monasca API
ARG monasca_output_plugin_tag=1.0.2
ARG monasca_output_plugin_url=https://github.com/monasca/fluentd-monasca/archive/$monasca_output_plugin_tag.tar.gz

RUN curl $monasca_output_plugin_url -o /tmp/fluentd-monasca.tar.gz \
    && tar -xvf /tmp/fluentd-monasca.tar.gz -C /tmp \
    && cd /tmp/fluentd-monasca-* \
    && td-agent-gem build fluentd-monasca-output.gemspec \
    && {{ macros.install_fluent_plugins(['fluentd-monasca-output-*.gem'], chain=True) }} \
    && rm -rf /tmp/fluentd*

{% endblock %}

{% block fluentd_footer %}{% endblock %}
{% block footer %}{% endblock %}

USER {{ fluentd_user }}
